package com.currency.mall.ui.my;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.content.ContextCompat;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.bigkoo.pickerview.OptionsPickerView;
import com.currency.mall.Constant;
import com.currency.mall.R;
import com.currency.mall.entity.BankCardBean;
import com.currency.mall.netWork.NetWork;
import com.mifengs.commonbase.base.BaseActivity;
import com.mifengs.commonbase.util.PreferenceUtils;
import com.mifengs.commonbase.util.StatusBarCompat;
import com.mifengs.commonbase.util.StatusBarUtils;
import com.mifengs.commonbase.util.ToastUitl;
import com.mifengs.commonbusines.entity.ResponseBean;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import rx.Observer;
import rx.android.schedulers.AndroidSchedulers;
import rx.schedulers.Schedulers;

/**
 * Created by Administrator on 2018/12/24.
 */

public class AddBankCardActivity extends BaseActivity {

    @BindView(R.id.tv_input_name)
    TextView tvInputName;
    @BindView(R.id.et_input_bank_car_code)
    EditText etInputBankCarCode;
    @BindView(R.id.tv_input_open_account_band)
    TextView tvInputOpenAccountBand;
    @BindView(R.id.et_input_open_account_address)
    EditText etInputOpenAccountAddress;
    @BindView(R.id.btn_save)
    Button btnSave;
    private List<String> bankNameList;
    private List<BankCardBean.BankNameInfo> bankCardBeans;
    private long bankId;
    private int status = 0; // 0  是从银行卡列表进去  1 是从提现进入

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_bankcard);
        ButterKnife.bind(this);
        StatusBarCompat.setStatusBarColor(this, ContextCompat.getColor(getContext(), R.color.title_color));
        StatusBarUtils.StatusBarLightMode(this);
        etInputBankCarCode.addTextChangedListener(textWatcher);
        tvInputOpenAccountBand.addTextChangedListener(textWatcher);
        tvInputName.setText( PreferenceUtils.getUserName(getContext()));
        status = getIntent().getIntExtra("status",0);
        bankNameList = new ArrayList<>();
        bankCardBeans = new ArrayList<>();
    }

    TextWatcher textWatcher = new TextWatcher() {
        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            String bankCard = tvInputOpenAccountBand.getText().toString().trim();
            String bankCode= etInputBankCarCode.getText().toString().trim();
            if (TextUtils.isEmpty(bankCard) || TextUtils.isEmpty(bankCode) || TextUtils.isEmpty(s)) {
                btnSave.setBackgroundResource(R.drawable.btn_bg);
                btnSave.getBackground().setAlpha(50);
                btnSave.setClickable(false);
            } else {
                btnSave.setBackgroundResource(R.drawable.btn_bg);
                btnSave.getBackground().setAlpha(250);
                btnSave.setClickable(true);
            }
        }

        @Override
        public void afterTextChanged(Editable s) {

        }
    };

    @OnClick({R.id.ll_back, R.id.btn_save,R.id.tv_input_open_account_band})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_back:
                finish();
                break;
            case R.id.btn_save:
                addBankCard();
                break;
            case R.id.tv_input_open_account_band:
                if(bankNameList.size()>0){
                    selectBank();
                }else{
                    getBank();
                }

                break;
        }
    }

    public void addBankCard() {
        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("name", tvInputName.getText().toString().trim());
        hashMap.put("cardId", etInputBankCarCode.getText().toString().trim());
        hashMap.put("bankId", bankId);
        hashMap.put("address", etInputOpenAccountAddress.getText().toString().trim());
        NetWork.getUserApi().addBankCard(hashMap)
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<ResponseBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(ResponseBean responseBean) {
                        if (responseBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            ToastUitl.show("添加成功", 200);
                            if(status == 0){
                                setResult(1001);
                            }else{
                                setResult(1002);
                            }

                            finish();
                        } else {
                            ToastUitl.show(responseBean.getMsg(), 200);
                        }
                    }
                });
    }

    public void getBank() {
        NetWork.getUserApi().bankList()
                .subscribeOn(Schedulers.io())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<BankCardBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {
                    }

                    @Override
                    public void onNext(BankCardBean bankCardBean) {
                        if (bankCardBean.getCode().equals(Constant.SUCCESS_CODE)) {
                            bankCardBeans = bankCardBean.getBanks();
                            for(int i = 0;i<bankCardBeans.size();i++){
                                bankNameList.add(bankCardBeans.get(i).getBankName());
                            }
                            selectBank();
                        } else {
                            ToastUitl.show(bankCardBean.getMsg(), 200);
                        }
                    }
                });
    }

    /**
     * 选择开户行
     */
    private void selectBank() {
        OptionsPickerView pvOptions = new OptionsPickerView.Builder(getContext(), new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3, View v) {
                //返回的分别是三个级别的选中位置
                tvInputOpenAccountBand.setText(bankNameList.get(options1));
                bankId =  Long.parseLong( bankCardBeans.get(options1).getId());

            }
        })
//                        .setSubmitText("确定")//确定按钮文字
//                        .setCancelText("取消")//取消按钮文字
//                        .setTitleText("城市选择")//标题
                .setSubCalSize(18)//确定和取消文字大小
//                        .setTitleSize(20)//标题文字大小
//                        .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(getResources().getColor(R.color.black_color))//确定按钮文字颜色
                .setCancelColor(getResources().getColor(R.color.optional_color))//取消按钮文字颜色
//                        .setTitleBgColor(0xFF333333)//标题背景颜色 Night mode
//                        .setBgColor(0xFF000000)//滚轮背景颜色 Night mode
//                        .setContentTextSize(18)//滚轮文字大小
//                        .setTextColorCenter(Color.BLUE)//设置选中项的颜色
                .setTextColorCenter(getResources().getColor(R.color.optional_color))//设置选中项的颜色
                .setLineSpacingMultiplier(2.0f)//设置两横线之间的间隔倍数
//                        .setLinkage(false)//设置是否联动，默认true
//                        .setLabels("省", "市", "区")//设置选择的三级单位
//                        .isCenterLabel(false) //是否只显示中间选中项的label文字，false则每项item全部都带有label。
//                        .setCyclic(false, false, false)//循环与否
//                        .setSelectOptions(1, 1, 1)  //设置默认选中项
//                        .setOutSideCancelable(false)//点击外部dismiss default true
//                        .isDialog(true)//是否显示为对话框样式
                .build();
        pvOptions.setPicker(bankNameList);
        pvOptions.show();
    }
}
